import _lodash from 'lodash';

_lodash.substr = function (str = '', startpos, length) {
  if (length < 0) {
    return str.substr(startpos, str.length + length);
  }
  return str.substr(startpos, length);
};
_lodash.swap = function (list, pos1, pos2) {
  const t = list[pos1];
  list[pos1] = list[pos2];
  list[pos2] = t;
};
_lodash.count = function (arr, iteratee) {
  return _lodash.filter(arr, iteratee).length;
};
_lodash.joinBy = function (arr, key, separator) {
  return _lodash.map(arr, key).join(separator);
};
_lodash.omitNil = function (obj) {
  return _lodash.omitBy(obj, _lodash.isNil);
};
_lodash.cloneDeepOmitNil = function (obj) {
  return _lodash.cloneDeep(_lodash.omitNil(obj));
};
_lodash.addToSet = function (arr, item) {
  if (!_lodash.includes(arr, item)) {
    arr.push(item);
  }
};
// m == n
_lodash.isArrayEqual = function (m = [], n = []) {
  return _lodash.difference(n, m).length === 0 && m.length === n.length;
};
// m 包含 n
_lodash.isContain = function (m = [], n = []) {
  return _lodash.difference(n, m).length === 0;
};
// m 真包含 n
_lodash.isTrueContain = function (m = [], n = []) {
  return _lodash.difference(n, m).length === 0 && m.length > n.length;
};
// _.in(1, [1, 2, 3])
// _.in(1, 1, 2, 3)
_lodash.in = function (v, ...l) {
  if (_lodash.isArray(l[0])) {
    l = _lodash.flatten(l);
  }
  return _lodash.includes(l, v);
};
_lodash.ifNullKey = function (obj, key, value) {
  if (_lodash.isNil(obj[key])) {
    obj[key] = value;
  }
  return obj;
};
_lodash.ifNull = function (v, defaultValue) {
  return _lodash.isNil(v) ? defaultValue : v;
};

export default _lodash;
